package com.liujit.standard.basis.core.config;

import com.alibaba.fastjson.JSON;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.lang.reflect.Type;

/**
 * @description: feign配置
 * @author: liujun
 * @create: 2021/3/24 5:29 下午
 **/
@Configuration
public class FeignConf implements Decoder {

    @Override
    public Object decode(Response response, Type type) throws IOException, FeignException {
        if (response.body() == null) {
            throw new DecodeException(response.status(), "no data response", response.request());
        }
        String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
        return JSON.parseObject(bodyStr, type);
    }
}
